package com.onlineMIS.ORM.DAO.headQ.finance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.base.BaseOrder;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceBill;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceCategory;
import com.onlineMIS.ORM.entity.headQ.supplier.finance.FinanceBillSupplier;
import com.onlineMIS.ORM.entity.headQ.supplier.purchase.PurchaseOrder;

@Repository
public class FinanceCategoryImpl extends BaseDAO<FinanceCategory> {

	    public List<FinanceCategory> getAllNormal(boolean cached){
	    	DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategory.class);
	    	criteria.add(Restrictions.eq("status",FinanceCategory.NORMAL_STATUS));
	    	criteria.addOrder(Order.asc("type"));
	    	return this.getByCritera(criteria, cached);
	    }
	    
	    /**
	     * 获取总部账户
	     * @param cached
	     * @return
	     */
	    public List<FinanceCategory> getHqAccount(boolean cached){
	    	DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategory.class);
	    	criteria.add(Restrictions.eq("belongTo", FinanceCategory.BELONG_TO_HQ));
	    	criteria.add(Restrictions.eq("status",FinanceCategory.NORMAL_STATUS));
	    	criteria.addOrder(Order.asc("sort"));
	    	return this.getByCritera(criteria, cached);
	    }
	
	    public FinanceCategory getByTypeId(int typeId){
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategory.class);
			criteria.add(Restrictions.eq("type", typeId));
			List<FinanceCategory> categories =  getByCritera(criteria, true);
			if (categories == null || categories.size() == 0)
				return null;
			else 
				return categories.get(0);
	    }
	    
	    /**
	     * id: typeId;
	     * value: financeCategory
	     * @return
	     */
	    public Map<Integer, FinanceCategory> getFinanceCategoryMap(){
	    	List<FinanceCategory> categories = getAllNormal(true);
	    	
	    	Map<Integer, FinanceCategory> categoryMap = new HashMap<Integer, FinanceCategory>();
	    	
	    	for (FinanceCategory financeCategory: categories){
	    		int typeId = financeCategory.getType();
	    		categoryMap.put(typeId, financeCategory);
	    	}
	    	return categoryMap;
	    }

	    /**
	     * 查询正常状态下支持转账的账户
	     * @return
	     */
		public List<FinanceCategory> selectIsTransferAccountsFc() {
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategory.class);
			criteria.add(Restrictions.eq("status", FinanceCategory.NORMAL_STATUS));
			criteria.add(Restrictions.eq("isTransferAccounts", FinanceCategory.SUPPORT_TRANSFER_ACCOUNTS));
			
			return getByCritera(criteria, false);
		}

		
		/**
		 * 判断是否是系统账目
		 * @param FinanceCategoryType
		 * @return
		 */
		public boolean isSystemAcct(int FinanceCategoryType) {
			if (FinanceCategoryType == FinanceCategory.INCREASE_DECREASE_ACCT_SUPPLIER_TYPE || FinanceCategoryType == FinanceCategory.INCREASE_DECREASE_ACCT_TYPE ||FinanceCategoryType == FinanceCategory.PREPAY_ACCT_SUPPLIER_TYPE ||FinanceCategoryType == FinanceCategory.PREPAY_ACCT_TYPE) 
			     return true;
			else 
				return false;
		}

	    /**
	     * get the preincomeItem
	     * @return
	     */
		public List<FinanceCategory> selectFcBelongTo(int belongTo,int belongTo2){
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategory.class);
			criteria.add(Restrictions.eq("belongTo", belongTo));
			criteria.add(Restrictions.eq("belongTo2", belongTo2));
			return getByCritera(criteria, true);
		}

		/**
		 * 根据类型获取账户
		 * @return
		 */
		public FinanceCategory selectByType(int type,int belongTo2) {
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategory.class);
			criteria.add(Restrictions.eq("type", type));
			criteria.add(Restrictions.eq("belongTo2", belongTo2));
			List<FinanceCategory> list = getByCritera(criteria, true);
			if (list.size() !=0) {
				return list.get(0);
			}else {
				return null;
			}			
		}

		
		/**
		 * 根据item_type查找总部账目
		 * @param itemTye
		 * @return
		 */
		public FinanceCategory selectByItemType(Integer itemTye,int belongTo2) {
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategory.class);
			criteria.add(Restrictions.eq("type", itemTye));
			criteria.add(Restrictions.eq("belongTo2", belongTo2));
			return getByCritera(criteria, true).get(0);
		}

		
		

}
